package cn.good.yan.sf.t6;

import java.util.Iterator;

/**
 * 6 队列 - 基于 链表实现的队列
 *
 * @author shengren.yan
 * @create 2024-07-21
 */
public class LinkListQueue<E> implements Queue<E>, Iterable<E> {

    static class Node<E> {
        private E value;
        private Node<E> next;

        public Node(E value, Node<E> next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node<E> head = new Node<>(null, null);
    private Node<E> tail = head;
    private int size = 0;
    private int num = Integer.MAX_VALUE;

    public LinkListQueue() {

    }

    public LinkListQueue(int num) {
        this.num = num;
    }

    {
        head.next = head;
    }

    // 向队列添加元素
    // head null 1 null
    // tail 1 null
    @Override
    public boolean offer(E e) {
        if (isFull()) {
            return false;
        }
        Node<E> node = new Node<>(e, head);
        tail.next = node;
        tail = node;
        size++;
        return true;
    }

    // 向队列移除元素
    @Override
    public E pool() {
        if (isEmpty()) {
            return null;
        }
        Node<E> first = head.next;
        head.next = first.next;
        if (tail == first) {
            tail = head;
        }
        size--;
        return first.value;
    }

    // 从队列头获取值，并不移除
    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        Node<E> first = head.next;
        return first.value;
    }

    // 检查队列是否为空，true就是空
    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    // 检查队列是否满了，true就是满了
    @Override
    public boolean isFull() {
        return size >= num;
    }

    // 遍历
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> n = head.next;

            @Override
            public boolean hasNext() {

                return n != head;
            }

            @Override
            public E next() {
                E value = n.value;
                n = n.next;
                return value;
            }
        };
    }

    public static void main(String[] args) {
        LinkListQueue<Integer> q = new LinkListQueue<>(5);
        System.out.println("移除元素：" + q.pool());
        System.out.println("获取头元素：" + q.peek());
        System.out.println("添加元素：" + q.offer(1));
        System.out.println("添加元素：" + q.offer(2));
        System.out.println("添加元素：" + q.offer(3));
        System.out.println("添加元素：" + q.offer(4));
        System.out.println("添加元素：" + q.offer(5));
        System.out.println("添加元素：" + q.offer(6));
        System.out.println("添加元素：" + q.offer(7));
        System.out.println("--------------");
        for (Integer i : q) {
            System.out.println(i);
        }
        System.out.println("获取头元素：" + q.peek());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("移除元素：" + q.pool());
        System.out.println("--------------");
        for (Integer i : q) {
            System.out.println(i);
        }
        System.out.println("--------------");
    }


}
